package com.zjh.reactive;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @Auther: zjh
 * @Date: 2025/11/2 - 11 - 02 - 11:56
 * @Description: com.zjh.reactive
 * @version: 1.0
 */
public class MonoDemo {
    public static void main1(String[] args) {
        Stream.of("1", "2", "3")
                .map(v -> Mono.just(v + " 👍"))
                .forEach(System.out::println);
        Disposable subscribe = Mono.just("liu huaqiang")
                .map(String::toUpperCase)
                .flatMap(v -> Mono.just(v + " 👍"))
                .filter(v -> v.length() > 5)
                .doOnNext(v -> System.out.println("处理中：" + v))
                .onErrorResume(e -> Mono.just("fallback"))
                .switchIfEmpty(Mono.just("空值替代"))
                .subscribe(
                        v -> System.out.println("结果：" + v),
                        e -> System.err.println("出错：" + e),
                        () -> System.out.println("完成")
                );
        Mono.just(List.of("1", "2", "3"))
                .map(s -> s.toString())
                .subscribe(System.out::println);
        Mono.from(new Publisher<Integer>() {
                    @Override
                    public void subscribe(Subscriber<? super Integer> subscriber) {
                        subscriber.onSubscribe(new Subscription() {
                            @Override
                            public void request(long l) {
                                subscriber.onNext(1);
                                subscriber.onComplete();
                            }

                            @Override
                            public void cancel() {

                            }
                        });
                    }
                })
                .subscribe(t -> System.out.println("收到消息：" + t),
                        e -> System.out.println("出错：" + e),
                        () -> System.out.println("完成")

                );


    }

    public static void main(String[] args) {
       Mono.defer(()->Mono.just("1")).subscribe(System.out::println);

    }
}
